package superAdmin;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.concurrent.TimeUnit;
/*
 学院的增删改查
 */
import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class AddCollege extends InitAdmin{

    @ParameterizedTest
    @CsvFileSource(resources = "../superadminLogin.csv")
    @Order(1)
    void login(String name,String password,String loginURL,String toURL) throws InterruptedException {

        //获取登录页
        webDriver.get(loginURL);
        webDriver.manage().window().maximize();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        System.out.println("username:"+name);
        System.out.println("password:"+password);
        //输入账号密码
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(1) > div > div > input")).sendKeys(name);
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(2) > div > div > input")).sendKeys(password);
        //点击登录
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(3) > div > button")).click();
        webDriver.manage().window().maximize();
        sleep(3000);

        String curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(toURL,curUrl);
        sleep(3000);

    }
    @ParameterizedTest
    @CsvSource("计科,1001")
    @Order(2)
    void addCollege(String collegeName,String  collegeId){
        //选择 添加学院
        webDriver.findElement(By.cssSelector("#schoolBox > div.tit > div:nth-child(2) > button:nth-child(2) > span")).click();
        //输入学院和编号
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editAcademy > div > div.el-dialog__body > div > form > div:nth-child(1) > div > div > input"))
                .sendKeys(collegeName);
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editAcademy > div > div.el-dialog__body > div > form > div:nth-child(2) > div > div > input"))
                .sendKeys(collegeId);
        //点击确定
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editAcademy > div > div.el-dialog__footer > div > button.el-button.btn.confirm-btn.el-button--primary.el-button--small"))
                .click();
    }
    @ParameterizedTest
    @CsvSource("计科2,1001")
    @Order(3)
    void modifyCollege(String collegeNameNew) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //选中新增的学院
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.right-content.manage-agency > div > div > div.academy-left.fl > div > div > div > div:nth-child(3)")).click();
        //点击编辑院系
        webDriver.findElement(By.cssSelector("#schoolBox > div.tit > div:nth-child(3) > button:nth-child(1)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //模拟鼠标操作
        webDriver.findElement(By.xpath("//*[@id=\"admin\"]/div[2]/div/div[4]/div/div[2]/div/form/div[1]/div/div/input"))
                .sendKeys(Keys.CONTROL,"A");
        sleep(1000);
        webDriver.findElement(By.xpath("//*[@id=\"admin\"]/div[2]/div/div[4]/div/div[2]/div/form/div[1]/div/div/input"))
                .sendKeys(Keys.CONTROL,"X");
        sleep(1000);
        //输入新明名
        webDriver.findElement(By.xpath("//*[@id=\"admin\"]/div[2]/div/div[4]/div/div[2]/div/form/div[1]/div/div/input"))
                .sendKeys(collegeNameNew);
        sleep(3000);
        //点击确定
       webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editAcademy > div > div.el-dialog__footer > div > button.el-button.btn.confirm-btn.el-button--primary.el-button--small"))
               .click();
    }
    @ParameterizedTest
    @CsvSource("计科2,1001")
    @Order(4)
    void deleteAndSearch(String collegeNameNew){
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //刷新页面
        webDriver.navigate().refresh();
        //选中刚才新添加的学院
        webDriver.findElement(By.xpath("/html/body/div/div[2]/div/div[2]/div/div/div[1]/div/div/div/div[3]/div[1]/div")).click();
        //点击 编辑 院系
        webDriver.findElement(By.cssSelector("#schoolBox > div.tit > div:nth-child(3) > button:nth-child(1)")).click();
        //点击删除
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editAcademy > div > div.el-dialog__footer > div > button.el-button.btn.delete-btn.el-button--primary.el-button--small"))
                .click();
        //确定 删除
        webDriver.findElement(By.cssSelector("body > div.el-message-box__wrapper > div > div.el-message-box__btns > button.el-button.el-button--default.el-button--small.el-button--primary.confirm-btn"))
                .click();
        //获取 页面所有的机构名元素 验证是否删除成功
        List<WebElement> colleges = webDriver.findElements(By.cssSelector("h1 > font"));
        //#el-collapse-head-3562 > h1 > font
        System.out.println(colleges.size());
        for (int i = 0; i < colleges.size(); i++) {
            System.out.println("当前学院机构："+colleges.get(i).getText());
            if (colleges.get(i).getText().equals(collegeNameNew)) {
                System.out.println("测试不通过!删除失败，依旧存在:"+colleges.get(i));
            }
        }

    }
}
